package com.sunfeng.chap2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 *    write  把缓冲区的内容写入到通道
 *    int write(ByteBuffer src)方法 是从通道当前位置开始写入的
 *
 */
public class Test1_1 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("d:\\abc.txt");
        FileChannel fileChannel = fos.getChannel();


        try {
            ByteBuffer byteBuffer = ByteBuffer.wrap("abcde".getBytes());
            System.out.println("A"+fileChannel.position());
            System.out.println(fileChannel.write(byteBuffer));
            System.out.println("B"+fileChannel.position());
            // 重绕缓冲区  重新进行
//            byteBuffer.rewind();
//            fileChannel.position(3);
           System.out.println(fileChannel.write(byteBuffer));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            fileChannel.close();
            fos.close();
        }
    }
}
